﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.Utilities;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.Singer
{
    public partial class CreateEditSinger : AdminPage
    {
        #region Khai báo
        private int singerId = 0;
        #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                singerId = ObjConvertUtility.ToInt32(Request.QueryString["singerId"]);
                if (!Page.IsPostBack)
                {
                    txtBirthDay.SelectedDate = DateTime.Now;
                    LoadData();
                    txtDescription.Height = 180;
                    txtDescription.Width = 460;
                    txtDescription.SetSecurityFilesGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                    txtDescription.SetSecurityFlashGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                    txtDescription.SetSecurityGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                    txtDescription.SetSecurityImageGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                    txtDescription.SetSecurityMediaGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                    txtDescription.SetSecurityTemplateGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                }
            }
            catch (Exception ex)
            {
            }
        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (singerId > 0)
                {
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;
                    SingerInfo info = SingerBL.Select(singerId);
                    if (info != null)
                    {
                        txtSingerName.Text = info.SingerName;
                        txtMobile.Text = info.Mobile;
                        txtDescription.Text = info.Description;
                        txtAddress.Text = info.Address;
                        txtImage.Text = info.Images;
                        txtKeyword.Text = info.Keyword;
                    }
                }
                else
                {
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region FillData
        void FillData(SingerInfo info)
        {
            info.SingerName = txtSingerName.Text;
            info.Mobile = txtMobile.Text;
            info.Description = txtDescription.Text;
            info.Address = txtAddress.Text;
            info.Keyword = txtKeyword.Text;
            info.SingerId = singerId;
            info.CreatedBy = CurrentUser.Username;
            info.CreatedOn = DateTime.Now;
            info.BirthDay = txtBirthDay.SelectedDate;
            info.Type = 0;
            
            info.Status = Const.show;
            if (txtImage.Text.Trim() != info.Images && txtImage.Text.Trim().Length > 0)// Nếu thay đổi ảnh -> Thuc hien Crop anh
            {
                info.Images = ConvertImage.SetAvatarThumbnailWidthDirection(Server.MapPath(txtImage.Text.Trim()), 65, "_singer");
            }
            else
            {
                if (txtImage.Text.Length > 0)
                {
                    info.Images = txtImage.Text.Trim();
                }
                else
                {
                    info.Images = "";
                }
            }


        }
        #endregion

        #region thêm mới
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                SingerInfo info = new SingerInfo();
                FillData(info);
                int i = SingerBL.Insert(info);
                if (i > 0)
                {
                    lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
                    ClearText();
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region Chỉnh sửa
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                SingerInfo info = new SingerInfo();
                FillData(info);
                SingerBL.Update(info);
                lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công";
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region ClearText
        void ClearText()
        {
            txtSingerName.Text = "";
            txtMobile.Text = "";
            txtDescription.Text = "";
            txtAddress.Text = "";
            txtImage.Text = "";
            txtKeyword.Text = "";
        }
        #endregion
    }
}